﻿#include "ThreeDSurfacePlotSidebarSettings.h"
#include "ui_ThreeDSurfacePlotSidebarSettings.h"
#include <QVBoxLayout>
#include <QObject>
#include <QLabel>
#include <QLineEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QWidget>
#include <QRegExp>
#include <QTabWidget>
#include <QTabBar>
#include <QScrollBar>
#include <QListView>
#include <QDebug>
#include <QIcon>
#include <QStandardItemModel>
#include <QListWidgetItem>
#include <QGroupBox>
#include <QCheckBox>
#include <QPushButton>
#include "ThreeDSurfacePlotSidebarData.h"
#include "FormulaEditorDialog.h"
#include "BaseModule/IBaseSignal.h"
#include "BaseModule/GlobalHelper.h"
#include "TextArrayMetaHander.h"
#include "BaseWidget/IdaDialogMsg.h"

namespace pst
{
    ThreeDSurfacePlotSidebarSettings::ThreeDSurfacePlotSidebarSettings(QWidget* parent)
        : QScrollArea(parent)
        , _titleFontSize{ 14 }
        , ui(new Ui::ThreeDSurfacePlotSidebarSettings())
        , m_enableStatus()
    {
        ui->setupUi(this);
        //设置基本布局属性
        setContentsMargins(0, 0, 0, 0);
        addTopTabPivotUI();
        ui->dockWidget_Data->setStyleSheet("QDockWidget > QWidget{border: 1px solid #EBEDEE;}");
        ui->dockWidget_DataSource->setStyleSheet("QDockWidget > QWidget{border: 1px solid #EBEDEE;}");
        ui->dockWidget_2->setStyleSheet("QDockWidget > QWidget{border: 1px solid #EBEDEE;}");
        ui->dockWidget_3->setStyleSheet("QDockWidget > QWidget{border: 1px solid #EBEDEE;}");
        ui->lineEdit_FormulaEditor->setPlaceholderText(QStringLiteral("self"));
        ui->scrollArea->setStyleSheet("QScrollArea > QWidget{border: 0px solid #EBEDEE;}");
        ui->listWidget_Data->setWordWrap(true);
        connect(ui->listWidget_Data, &QListWidget::itemChanged, this, &ThreeDSurfacePlotSidebarSettings::slot_itemCheckStateChanged);

        connectSignalsAndSlots();
        hideComponents();
        m_enableStatus.resize(8);
    }

    ThreeDSurfacePlotSidebarSettings::~ThreeDSurfacePlotSidebarSettings()
    {
    }

    void ThreeDSurfacePlotSidebarSettings::setMainWiondowId(int id)
    {
        m_mainWindowId = id;
    }


    QListWidgetItem* ThreeDSurfacePlotSidebarSettings::addData(InfomationGraph3DBasical* info)
    {
        QString curveName = info->m_name;
        QListWidgetItem* listItem = new QListWidgetItem(curveName);
        listItem->setFlags(listItem->flags() | Qt::ItemIsUserCheckable);
        listItem->setCheckState(Qt::Checked);
        ui->listWidget_Data->addItem(listItem);

        m_allAddedDataList.append(qMakePair(info, listItem));
        return listItem;
    }

    void ThreeDSurfacePlotSidebarSettings::updateInfoDataAndInterfaceToUI(ThreeDSurfacePlotSidebarData* data)
    {
        //disconnectSignalsAndSlots();
        QSignalBlocker blocker(ui->comboBox_valueData);
        QSignalBlocker blocker2(ui->comboBox_FixedValue);
        QSignalBlocker blocker3(ui->checkBox_Normalize);
        QSignalBlocker blocker4(ui->checkBox_DB);
        QSignalBlocker blocker5(ui->listWidget_Data);

        clearControlPlane();

        m_currentItemData = data;
        updateInfoValueToUI(data);
        //ui->checkBox_DB->setEnabled(true);
        ui->checkBox_Normalize->setEnabled(true);

        //connectSignalsAndSlots();
    }

    void ThreeDSurfacePlotSidebarSettings::updateInfoValueToUI(ThreeDSurfacePlotSidebarData* data)
    {
        updateGroupRadioButtonUI(data);

        auto valueList = data->getValueGroupNameList();
        auto frequencyList = data->getFrequencyList();
        for (const auto i : frequencyList)
        {
            ui->comboBox_FixedValue->addItem(QString::number(i));
        }
        ui->comboBox_FixedValue->setCurrentIndex(data->getCurrentFrequencyIndex());

        for (const auto& i : valueList)
        {
            ui->comboBox_valueData->addItem(i);
        }
        ui->comboBox_valueData->setCurrentIndex(data->getCurrentValueGroupNameIndex());
        ui->textEdit_DataSource->setText(data->getDataSource());
        //ui->checkBox_DB->setChecked(data->getIsLog());
        ui->checkBox_Normalize->setChecked(data->getIsNormalize());
    }

    void ThreeDSurfacePlotSidebarSettings::setControlPanelEditable(bool enable)
    {
        //enable?
        //ui->listWidget_Data->setSelectionMode(QAbstractItemView::SingleSelection):
        //ui->listWidget_Data->setSelectionMode(QAbstractItemView::NoSelection);

        ui->comboBox_FixedValue->setEnabled(enable);
        ui->comboBox_valueData->setEnabled(enable);
        ui->comboBox_categryValue->setEnabled(enable);

        ui->radioButton_Amplitude->setEnabled(enable);     // 幅度
        ui->radioButton_Phase->setEnabled(enable);         // 相位
        ui->radioButton_RealPart->setEnabled(enable);      // 实部
        ui->radioButton_ImaginaryPart->setEnabled(enable); // 虚部

        ui->checkBox_DB->setEnabled(enable);
        ui->checkBox_Normalize->setEnabled(enable);
    }

    QListWidget* ThreeDSurfacePlotSidebarSettings::getCurveNameListWidget()
    {
        return ui->listWidget_Data;
    }

    void ThreeDSurfacePlotSidebarSettings::setControlPlaneToAnimationState(bool isAnimationState)
    {
        if (isAnimationState)
        {
            disconnectSignalsAndSlots();

            m_enableStatus[0] = ui->comboBox_FixedValue->isEnabled();
            m_enableStatus[1] = ui->comboBox_valueData->isEnabled();
            m_enableStatus[2] = ui->radioButton_Amplitude->isEnabled();
            m_enableStatus[3] = ui->radioButton_Phase->isEnabled();
            m_enableStatus[4] = ui->radioButton_RealPart->isEnabled();
            m_enableStatus[5] = ui->radioButton_ImaginaryPart->isEnabled();
            m_enableStatus[6] = ui->checkBox_DB->isEnabled();
            m_enableStatus[7] = ui->checkBox_Normalize->isEnabled();

            setControlPanelEditable(false);
        }
        else
        {
            connectSignalsAndSlots();

            ui->comboBox_FixedValue->setEnabled(m_enableStatus[0]);
            ui->comboBox_valueData->setEnabled(m_enableStatus[1]);
            ui->radioButton_Amplitude->setEnabled(m_enableStatus[2]);
            ui->radioButton_Phase->setEnabled(m_enableStatus[3]);
            ui->radioButton_RealPart->setEnabled(m_enableStatus[4]);
            ui->radioButton_ImaginaryPart->setEnabled(m_enableStatus[5]);
            ui->checkBox_DB->setEnabled(m_enableStatus[6]);
            ui->checkBox_Normalize->setEnabled(m_enableStatus[7]);
        }
    }

    void ThreeDSurfacePlotSidebarSettings::pyCurveNameClicked(QList<int> selectedIndexs)
    {
        QList<QListWidgetItem*> allSelectedItem;
        {
            QSignalBlocker blocker(ui->listWidget_Data);

            for (int i = 0; i < selectedIndexs.size(); i++)
            {
                QListWidgetItem* item = ui->listWidget_Data->item(selectedIndexs.at(i));
                item->setSelected(true);
                allSelectedItem.append(item);
            }
        }
        if (allSelectedItem.isEmpty())
        {
            disconnectSignalsAndSlots();
            clearControlPlane();
            connectSignalsAndSlots();
            return;
        }
        ui->pushButton_FormulaEditor->setEnabled(true);
        emit signal_chosenItemsChanged(allSelectedItem);
    }

    void ThreeDSurfacePlotSidebarSettings::pyFrequencyChanged(int index)
    {
        {
            QSignalBlocker blocker(ui->comboBox_FixedValue);
            ui->comboBox_FixedValue->setCurrentIndex(index);
        }
        m_currentItemData->setCurrentFrequencyIndex(index);
        emit signal_updateActorOriginalData(m_currentItemData);
    }

    void ThreeDSurfacePlotSidebarSettings::pyResultDataChanged(int index)
    {
        {
            QSignalBlocker blocker(ui->comboBox_valueData);
            ui->comboBox_valueData->setCurrentIndex(index);
        }

        m_currentItemData->setCurrentValueGroupNameIndex(index);
        updateGroupRadioButtonUI(m_currentItemData);

        emit signal_updateActorValue(m_currentItemData);
    }

    void ThreeDSurfacePlotSidebarSettings::pyDataOprationChanged(bool isLoged, bool isNormalized)
    {
        {
            QSignalBlocker blocker(ui->checkBox_DB);
            ui->checkBox_DB->setChecked(isLoged);
            QSignalBlocker blocker2(ui->checkBox_Normalize);
            ui->checkBox_Normalize->setChecked(isNormalized);
        }
        m_currentItemData->setIsLog(isLoged);
        m_currentItemData->setIsNormalize(isNormalized);
        emit signal_updateActorValue(m_currentItemData);
    }

    void ThreeDSurfacePlotSidebarSettings::pyItemCheckStateChanged(int itemIndex, bool visible)
    {
        auto item = ui->listWidget_Data->item(itemIndex);
        {
            QSignalBlocker blocker(ui->listWidget_Data);
            item->setCheckState(visible ? Qt::Checked : Qt::Unchecked);
        }
        siganl_showHideItemActor(item);
    }

    void ThreeDSurfacePlotSidebarSettings::pyDelete3DSurfacePlots(QString selectedIndexs)
    {
        QStringList rowIndexsStr = selectedIndexs.split("_");
        QList<QListWidgetItem*> itemList;
        for (QString rowIndexStr : rowIndexsStr)
        {
            int rowIndex = rowIndexStr.toInt();
            itemList.append(ui->listWidget_Data->item(rowIndex));
        }
        emit signal_deleteChosenGraphs(itemList); 
    }

    void ThreeDSurfacePlotSidebarSettings::pyThreeDSurfaceCurveDataFileterRadioButtonClicked(int windowId, int index)
    {
        switch (index)
        {
        case 0:
            on_radioButton_Amplitude_clicked(false);
            break;
        case 1:
            on_radioButton_Phase_clicked(false);
            break;
        case 2:
            on_radioButton_RealPart_clicked(false);
            break;
        case 3:
            on_radioButton_ImaginaryPart_clicked(false);
            break;
        default:
            break;
        }
    }

    void ThreeDSurfacePlotSidebarSettings::addTopTabPivotUI()
    {
        addCurveListUI();
        addDataOptrationUI();
    }

    void ThreeDSurfacePlotSidebarSettings::addCurveListUI()
    {
        ui->listWidget_Data->setFixedHeight(150);
        ui->listWidget_Data->setSelectionMode(QAbstractItemView::ExtendedSelection);
        ui->listWidget_Data->setEditTriggers(QAbstractItemView::NoEditTriggers);
        ui->listWidget_Data->setSelectionMode(QAbstractItemView::SingleSelection);

        //connect(ui->listWidget_Data->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ThreeDSurfacePlotSidebarSettings::slot_curveNameClicked);
        //connect(ui->listWidget_Data, &QListWidget::itemChanged, this, &ThreeDSurfacePlotSidebarSettings::slot_itemCheckStateChanged);
        //connect(ui->listWidget_Data, &QListWidget::itemDoubleClicked, this, &ThreeDSurfacePlotSidebarSettings::slot_itemDoubleClicked);
        //connect(ui->listWidget_Data->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, &ThreeDSurfacePlotSidebarSettings::signal_renameCheck);

        ui->listWidget_Data->setContextMenuPolicy(Qt::CustomContextMenu);
        //QObject::connect(ui->listWidget_Data, &QListWidget::customContextMenuRequested,
        //    this, &ThreeDSurfacePlotSidebarSettings::slot_addCustomContextMenu);
    }

    void ThreeDSurfacePlotSidebarSettings::addDataOptrationUI()
    {
        ui->checkBox_DB->setEnabled(false);
        ui->checkBox_Normalize->setEnabled(false);
        ui->checkBox_MathOperation->setEnabled(false);
        ui->lineEdit_FormulaEditor->setReadOnly(true);
        ui->pushButton_FormulaEditor->setEnabled(false);

    }

    void ThreeDSurfacePlotSidebarSettings::hideComponents()
    {
        ui->label_categryValue->setVisible(false);
        ui->comboBox_categryValue->setVisible(false);
        ui->checkBox_MathOperation->setVisible(false);
        ui->lineEdit_FormulaEditor->setVisible(false);
        ui->pushButton_FormulaEditor->setVisible(false);

        ui->radioButton_Amplitude->setEnabled(false);     // 幅度
        ui->radioButton_Phase->setEnabled(false);         // 相位
        ui->radioButton_RealPart->setEnabled(false);      // 实部
        ui->radioButton_ImaginaryPart->setEnabled(false); // 虚部
    }

    void ThreeDSurfacePlotSidebarSettings::updateGroupRadioButtonUI(ThreeDSurfacePlotSidebarData* data)
    {
        QSignalBlocker blocker1(ui->radioButton_Amplitude);
        QSignalBlocker blocker2(ui->radioButton_Phase);
        QSignalBlocker blocker3(ui->radioButton_RealPart);
        QSignalBlocker blocker4(ui->radioButton_ImaginaryPart);
        QSignalBlocker blocker5(ui->checkBox_DB);

        auto textParser = data->getTextFileParser();
        if (textParser == nullptr)
        {
            qDebug() << "TextFileParser is null, cannot update UI.";
            return;
        }
        int currentValueGroupNameIndex = data->getCurrentValueGroupNameIndex();
        int currentComponentIndex = data->getCurrentComponentIndex();

        int realIndex = textParser->getRealValueIndex(currentValueGroupNameIndex, currentComponentIndex);
        auto metaData = textParser->getDataInfoByRealValueIndex(realIndex);
        int resultParaType = metaData.resultParaType;
        int resultDataType = metaData.resultDataType;

        QList<QRadioButton*> radios = {
            ui->radioButton_RealPart, ui->radioButton_ImaginaryPart,
            ui->radioButton_Amplitude, ui->radioButton_Phase };

        // 简化代码，先禁用所有按钮
        for (auto* rb : radios)
        {
            rb->setAutoExclusive(true);
            rb->setEnabled(true);
        }
        // 简化代码，先禁止取对数
        ui->checkBox_DB->setEnabled(false);
        ui->checkBox_DB->setChecked(false);

        auto enableRadios = [this, &radios]() {
            for (auto* rb : radios)
            {
                rb->setAutoExclusive(true);
                rb->setEnabled(true);
            }};

        switch (resultDataType)
        {
        case 0: // 0:实部
        {
            enableRadios();
            ui->radioButton_RealPart->setChecked(true);
            break;
        }
        case 1:// 1:虚部
        {
            enableRadios();
            ui->radioButton_ImaginaryPart->setChecked(true);
            break;
        }
        case 2:// 2:幅度
        {
            enableRadios();
            ui->radioButton_Amplitude->setChecked(true);
            {
                QSignalBlocker blocker(ui->checkBox_DB);
                ui->checkBox_DB->setEnabled(true);
                ui->checkBox_DB->setChecked(data->getIsLog());
            }
            break;
        }
        case 3:// 3:相位
        {
            enableRadios();
            ui->radioButton_Phase->setChecked(true);
            break;
        }
        case 4://标量
        {
            // 先关闭 autoExclusive，才能把所有都取消选中
            for (auto* rb : radios)
            {
                rb->setAutoExclusive(false);   // 允许“同时不选”
                rb->setChecked(false);         // 取消选中
                rb->setEnabled(false);         // 禁用
                ui->checkBox_DB->setEnabled(true);
                ui->checkBox_DB->setChecked(data->getIsLog());
            }
            break;
        }
        default:
            break;
        }
    }

    void ThreeDSurfacePlotSidebarSettings::clearControlPlane()
    {
        QSignalBlocker blocker(ui->listWidget_Data);
        QSignalBlocker blocker2(ui->comboBox_FixedValue);
        QSignalBlocker blocker3(ui->comboBox_valueData);
        QSignalBlocker blocker4(ui->checkBox_Normalize);

        ui->comboBox_valueData->clear();
        ui->comboBox_FixedValue->clear();

        ui->checkBox_DB->setChecked(false);
        ui->checkBox_DB->setEnabled(false);
        ui->checkBox_Normalize->setChecked(false);
        ui->checkBox_Normalize->setEnabled(false);

        QSignalBlocker blocker5(ui->radioButton_RealPart);
        QSignalBlocker blocker6(ui->radioButton_ImaginaryPart);
        QSignalBlocker blocker7(ui->radioButton_Amplitude);
        QSignalBlocker blocker8(ui->radioButton_Phase);

        QList<QRadioButton*> radios = {
        ui->radioButton_RealPart, ui->radioButton_ImaginaryPart,
        ui->radioButton_Amplitude, ui->radioButton_Phase };

        for (auto* rb : radios)
        {
            rb->setAutoExclusive(false);   // 允许“同时不选”
            rb->setChecked(false);         // 取消选中
            rb->setEnabled(false);         // 取消选中
        }
    }

    //选中曲线后，判断所有选中曲线的信息，根据信息设置下面框的内内容
    void ThreeDSurfacePlotSidebarSettings::slot_curveNameClicked()
    {
        auto allSelectedItem = ui->listWidget_Data->selectedItems();
        QString dataSource;
        bool _isLegalSource = true;
        QList<int> selectedIndexes;
        for (QListWidgetItem* item : allSelectedItem)
        {
            int row = ui->listWidget_Data->row(item);
            selectedIndexes.append(row);
            if (_isLegalSource)
            {
                // 获取数据来源（项目树）
                QString _itemText;
                emit signal_getSidebarCurveDataSource(item, _itemText);
                if (dataSource.isEmpty() || dataSource.contains(_itemText))
                    dataSource = _itemText;
                else
                {
                    dataSource = "";
                    _isLegalSource = false;
                }
            }
        }
        ui->textEdit_DataSource->setText(dataSource);

        //TODO python发送选中下标
        QString sselectedIndexs = GlobalHelperInstance->convertIntListToQString(selectedIndexes);

        QStringList _pyCodes{};
        _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
        _pyCodes += QString("sidebarsettingspy.curveNameClicked(%1,\"%2\")").arg((int)m_mainWindowId).arg(sselectedIndexs);
        emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
        pyCurveNameClicked(selectedIndexes);
    }

    void ThreeDSurfacePlotSidebarSettings::on_pushButton_FormulaEditor_clicked()
    {
        QStringList _SelectionCurve{
            QStringLiteral("曲线1"),
        };
        auto _formulaEditorDialog = new FormulaEditorDialog();
        _formulaEditorDialog->setCurveList(_SelectionCurve);
        auto _ok = _formulaEditorDialog->execWindow();
        if (_ok)
        {
            auto _formulaContent = _formulaEditorDialog->getFormulaContent();
            ui->lineEdit_FormulaEditor->setText(_formulaContent);
        }
    }
    void ThreeDSurfacePlotSidebarSettings::on_radioButton_Amplitude_clicked(bool savePy)
    {
        auto _isClicked = ui->radioButton_Amplitude->isChecked();
        ui->radioButton_Amplitude->setCheckable(true);
        m_currentItemData->setCurrentComponentIndex(0);
        {
            QSignalBlocker blocker(ui->checkBox_DB);
            ui->checkBox_DB->setEnabled(true);
            ui->checkBox_DB->setChecked(m_currentItemData->getIsLog());
        }
        if (savePy)
        {
            QStringList _pyCodes{};
            _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
            _pyCodes += QString("sidebarsettingspy.threeDSurfaceCurveDataFileterRadioButtonClicked(%1,%2)").arg((int)m_mainWindowId).arg(0);
            emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
        }
        emit signal_updateActorValue(m_currentItemData);
    }
    void ThreeDSurfacePlotSidebarSettings::on_radioButton_Phase_clicked(bool savePy)
    {
        auto _isClicked = ui->radioButton_Phase->isChecked();
        ui->radioButton_Phase->setChecked(true);
        m_currentItemData->setCurrentComponentIndex(1);
        {
            //m_currentItemData->setIsLog(false);
            QSignalBlocker blocker(ui->checkBox_DB);
            ui->checkBox_DB->setEnabled(false);
            ui->checkBox_DB->setChecked(false);
        }
        if (savePy)
        {
            QStringList _pyCodes{};
            _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
            _pyCodes += QString("sidebarsettingspy.threeDSurfaceCurveDataFileterRadioButtonClicked(%1,%2)").arg((int)m_mainWindowId).arg(1);
            emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
        }
        emit signal_updateActorValue(m_currentItemData);
    }
    void ThreeDSurfacePlotSidebarSettings::on_radioButton_RealPart_clicked(bool savePy)
    {
        auto _isClicked = ui->radioButton_RealPart->isChecked();

        ui->radioButton_RealPart->setChecked(true);

        m_currentItemData->setCurrentComponentIndex(2);
        {
            //m_currentItemData->setIsLog(false);
            QSignalBlocker blocker(ui->checkBox_DB);
            ui->checkBox_DB->setEnabled(false);
            ui->checkBox_DB->setChecked(false);
        }
        if (savePy)
        {
            QStringList _pyCodes{};
            _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
            _pyCodes += QString("sidebarsettingspy.threeDSurfaceCurveDataFileterRadioButtonClicked(%1,%2)").arg((int)m_mainWindowId).arg(2);
            emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
        }
        emit signal_updateActorValue(m_currentItemData);
    }
    void ThreeDSurfacePlotSidebarSettings::on_radioButton_ImaginaryPart_clicked(bool savePy)
    {
        auto _isClicked = ui->radioButton_ImaginaryPart->isChecked();
        ui->radioButton_ImaginaryPart->setChecked(true);
        m_currentItemData->setCurrentComponentIndex(3);
        {
            //m_currentItemData->setIsLog(false);
            QSignalBlocker blocker(ui->checkBox_DB);
            ui->checkBox_DB->setEnabled(false);
            ui->checkBox_DB->setChecked(false);
        }
        if (savePy)
        {
            QStringList _pyCodes{};
            _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
            _pyCodes += QString("sidebarsettingspy.threeDSurfaceCurveDataFileterRadioButtonClicked(%1,%2)").arg((int)m_mainWindowId).arg(3);
            emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
        }
        emit signal_updateActorValue(m_currentItemData);
    }

    void ThreeDSurfacePlotSidebarSettings::slot_itemCheckStateChanged(QListWidgetItem* item)
    {
        auto index = ui->listWidget_Data->row(item);
        auto visible = item->checkState() == Qt::Checked ? true : false;

        QStringList _pyCodes{};
        _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
        _pyCodes += QString("sidebarsettingspy.curveNameItemCheckStateChanged(%1,%2,%3)").arg((int)m_mainWindowId).arg((int)index).arg(visible);
        emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
        pyItemCheckStateChanged(index, visible);
    }

    void ThreeDSurfacePlotSidebarSettings::slot_itemDoubleClicked(QListWidgetItem* item)
    {
        qDebug() << "Item double-clicked:" << item->text();
        siganl_graphNameDoubleClicked(item);
    }

    void ThreeDSurfacePlotSidebarSettings::slot_resultDataChanged(int index)
    {
        m_currentItemData->setCurrentValueGroupNameIndex(index);
        updateGroupRadioButtonUI(m_currentItemData);
        QStringList _pyCodes{};
        _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
        _pyCodes += QString("sidebarsettingspy.resultDataChanged(%1,%2)").arg((int)m_mainWindowId).arg((int)index);
        emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
        emit signal_updateActorValue(m_currentItemData);
    }

    void ThreeDSurfacePlotSidebarSettings::slot_frequencyChanged(int index)
    {
        QStringList _pyCodes{};
        _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
        _pyCodes += QString("sidebarsettingspy.frequencyChanged(%1,%2)").arg((int)m_mainWindowId).arg((int)index);
        emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true,false);
        pyFrequencyChanged(index);
    }

    void ThreeDSurfacePlotSidebarSettings::slot_normalizeClicked(bool checked)
    {
        auto isLoged = ui->checkBox_DB->isChecked();
        auto isNormalized = ui->checkBox_Normalize->isChecked();

        QStringList _pyCodes{};
        _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
        _pyCodes += QString("sidebarsettingspy.setDataOprationChanged(%1,%2,%3)").arg((int)m_mainWindowId).arg((int)isLoged).arg((int)isNormalized);
        emit IBaseSignalInstance->signal_execPYCode(_pyCodes,true,false);
        pyDataOprationChanged(isLoged, isNormalized);

        //m_currentItemData->setIsNormalize(checked);
        //emit signal_updateActorOriginalData(m_currentItemData);
    }

    void ThreeDSurfacePlotSidebarSettings::slot_logClicked(bool checked)
    {
        auto isLoged = ui->checkBox_DB->isChecked();
        auto isNormalized = ui->checkBox_Normalize->isChecked();

        QStringList _pyCodes{};
        _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
        _pyCodes += QString("sidebarsettingspy.setDataOprationChanged(%1,%2,%3)").arg((int)m_mainWindowId).arg((int)isLoged).arg((int)isNormalized);
        emit IBaseSignalInstance->signal_execPYCode(_pyCodes);
        pyDataOprationChanged(isLoged, isNormalized);

        //m_currentItemData->setIsLog(checked);
        //emit signal_updateActorOriginalData(m_currentItemData);
    }

    void ThreeDSurfacePlotSidebarSettings::connectSignalsAndSlots()
    {
        connect(ui->comboBox_valueData, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &ThreeDSurfacePlotSidebarSettings::slot_resultDataChanged);
        connect(ui->comboBox_FixedValue, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ThreeDSurfacePlotSidebarSettings::slot_frequencyChanged);
        connect(ui->checkBox_Normalize, &QCheckBox::clicked, this, &ThreeDSurfacePlotSidebarSettings::slot_normalizeClicked);
        connect(ui->checkBox_DB, &QCheckBox::clicked, this, &ThreeDSurfacePlotSidebarSettings::slot_logClicked);

        connect(ui->listWidget_Data->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ThreeDSurfacePlotSidebarSettings::slot_curveNameClicked);
        //connect(ui->listWidget_Data, &QListWidget::itemChanged, this, &ThreeDSurfacePlotSidebarSettings::slot_itemCheckStateChanged);
        connect(ui->listWidget_Data, &QListWidget::itemDoubleClicked, this, &ThreeDSurfacePlotSidebarSettings::slot_itemDoubleClicked, Qt::UniqueConnection);
        connect(ui->listWidget_Data->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, &ThreeDSurfacePlotSidebarSettings::signal_renameCheck);

        QObject::connect(ui->listWidget_Data, &QListWidget::customContextMenuRequested,
            this, &ThreeDSurfacePlotSidebarSettings::slot_addCustomContextMenu);
    }

    void ThreeDSurfacePlotSidebarSettings::disconnectSignalsAndSlots()
    {
        disconnect(ui->comboBox_valueData, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &ThreeDSurfacePlotSidebarSettings::slot_resultDataChanged);
        disconnect(ui->comboBox_FixedValue, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ThreeDSurfacePlotSidebarSettings::slot_frequencyChanged);
        disconnect(ui->checkBox_Normalize, &QCheckBox::clicked, this, &ThreeDSurfacePlotSidebarSettings::slot_normalizeClicked);
        disconnect(ui->checkBox_DB, &QCheckBox::clicked, this, &ThreeDSurfacePlotSidebarSettings::slot_logClicked);

        disconnect(ui->listWidget_Data->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ThreeDSurfacePlotSidebarSettings::slot_curveNameClicked);
        //disconnect(ui->listWidget_Data, &QListWidget::itemChanged, this, &ThreeDSurfacePlotSidebarSettings::slot_itemCheckStateChanged);
        disconnect(ui->listWidget_Data, &QListWidget::itemDoubleClicked, this, &ThreeDSurfacePlotSidebarSettings::slot_itemDoubleClicked);
        disconnect(ui->listWidget_Data->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, &ThreeDSurfacePlotSidebarSettings::signal_renameCheck);

        QObject::disconnect(ui->listWidget_Data, &QListWidget::customContextMenuRequested,
            this, &ThreeDSurfacePlotSidebarSettings::slot_addCustomContextMenu);
    }

    void ThreeDSurfacePlotSidebarSettings::slot_addCustomContextMenu(const QPoint& pos)
    {
        auto selectedItems = ui->listWidget_Data->selectedItems();
        if (selectedItems.isEmpty())
        {
            return;
        }

        QMenu contextMenu(this);
        QAction* deleteChosenGraphs = contextMenu.addAction("删除选中的图像");
        QObject::connect(deleteChosenGraphs, &QAction::triggered, this,
            [=]() {
                
                QList<QListWidgetItem*> itemList = ui->listWidget_Data->selectedItems();

                QString selectedIndexs;
                for (QListWidgetItem *item : itemList)
                {
                    int rowIndex = ui->listWidget_Data->row(item);
                    selectedIndexs.append("_").append(QString::number(rowIndex));
                }
                selectedIndexs = "'" + selectedIndexs.right(selectedIndexs.length() - 1) + "'";
                QStringList _pyCodes{};
                _pyCodes += QString("sidebarsettingspy = PostProcessing.ThreeDSurfacePlotSidebarSettingsPy()");
                _pyCodes += QString("sidebarsettingspy.delete3DSurfacePlots(%1,%2)").arg((int)m_mainWindowId).arg(selectedIndexs);
                qDebug() << "ThreeDSurfacePlotSidebarSettings::delete3DSurfacePlots---" << selectedIndexs;
                emit IBaseSignalInstance->signal_execPYCode(_pyCodes, true, false);
                emit signal_deleteChosenGraphs(ui->listWidget_Data->selectedItems()); 
            });

        if (selectedItems.size() == 1)
        {
            QAction* renameGraphs = contextMenu.addAction("重命名");
            QObject::connect(renameGraphs, &QAction::triggered, this, &ThreeDSurfacePlotSidebarSettings::slot_renameItem);
        }

        contextMenu.exec(ui->listWidget_Data->mapToGlobal(pos));
    }

    void ThreeDSurfacePlotSidebarSettings::slot_renameItem()
    {
        auto selectedItems = ui->listWidget_Data->selectedItems();
        if (selectedItems.size() != 1)
        {
            return;
        }

        auto item = selectedItems[0];
        item->setFlags(item->flags() | Qt::ItemIsEditable);
        ui->listWidget_Data->editItem(item);
    }
}
